package cn.liontalk.rabbitmqhelloworld.service.impl;

import cn.liontalk.rabbitmqhelloworld.config.RabbitMQConstant;
import cn.liontalk.rabbitmqhelloworld.domin.User;
import cn.liontalk.rabbitmqhelloworld.service.RabbitMQService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RabbitMQServiceImpl implements RabbitMQService {

    private static Logger logger = LoggerFactory.getLogger(RabbitMQServiceImpl.class);

    //日期格式化
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Autowired
    ObjectMapper objectMapper;


    //https://blog.csdn.net/A232222/article/details/112308585  消息的接收
    @Override
    public String sendMessage(String str) {
        try {
            // String msgId = UUID.randomUUID().toString().replace("-", "").substring(0, 32);
            //  String sendTime = sdf.format(new Date());
//            Map<String, Object> map = new HashMap<>();
//            map.put("msgId", msgId);
//            map.put("sendTime", sendTime);
//            map.put("msg", message);
//            MessageProperties messageProperties = new MessageProperties();
//            messageProperties.setMo();
//            messageProperties.setContentType(JSON.toJSONString(map));
            //
            for (int i = 0; i < 20; i++) {

                User user = new User("1" + i, str + i, 23);

                Message message = MessageBuilder.withBody(objectMapper.writeValueAsBytes(user))
                        .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                        .setContentEncoding("utf-8")
                        .setMessageId(new Random().nextInt(10) + "")
                        .build();
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                message.getMessageProperties().setCorrelationId(new Random().nextInt(10) + "");
                logger.info("发送消息=====================》》》》》");
                logger.info(message.getBody().toString());
                CorrelationData correlationData = new CorrelationData();
                correlationData.setId(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend(RabbitMQConstant.RABBITMQ_DEMO_DIRECT_EXCHANGE,
                        RabbitMQConstant.RABBITMQ_DEMO_DIRECT_ROUTING, message,correlationData);
            }

            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }


}
